//
//  FQDCommentCell.m
//  FakeQDaily
//
//  Created by mewxiv on 8/30/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDCommentCell.h"
#import "FQDRestModel.h"
#import "FQDPraiseHistory.h"
#import "FQDAccountManager.h"
#import "NSDate+FQDExtension.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

static CGFloat const kAvatarImageSize = 33.0f;

@interface FQDCommentCell ()

// 头像图标
@property (nonatomic, strong) UIImageView *avatarImageView;
// 评论者名字label
@property (nonatomic, strong) UILabel *authorNameLabel;
// 评论时间
@property (nonatomic, strong) UILabel *dateLabel;
// 点赞按钮
@property (nonatomic, strong) UIButton *likeButton;
// 点赞数
@property (nonatomic, strong) UILabel *likeNumLabel;
// 评论内容label
@property (nonatomic, strong) UILabel *contentLabel;
// 是否点赞了评论
@property (nonatomic, assign) BOOL liked;

@end

@implementation FQDCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    _avatarImageView = [[UIImageView alloc] init];
    _avatarImageView.clipsToBounds = YES;
    _avatarImageView.layer.cornerRadius = kAvatarImageSize / 2;
    [self.contentView addSubview:_avatarImageView];
    
    _authorNameLabel = [[UILabel alloc] init];
    _authorNameLabel.font = FQDBoldFontWithSize(14.0f);
    [self.contentView addSubview:_authorNameLabel];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textColor = FQDCommentDateTextColor;
    [self.contentView addSubview:_dateLabel];
    
    
    UIImage *likeImage = [UIImage imageNamed:@"commentLike"];
    _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_likeButton setBackgroundImage:likeImage forState:UIControlStateNormal];
    [_likeButton addTarget:self action:@selector(likeComment:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_likeButton];
    
    _likeNumLabel = [[UILabel alloc] init];
    _likeNumLabel.font = FQDRegularFontWithSize(14.0f);
    _likeNumLabel.textColor = RGB(121, 121, 121);
    [self.contentView addSubview:_likeNumLabel];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = FQDRegularFontWithSize(14.0f);
    [self.contentView addSubview:_contentLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(weakSelf);
    }];
    
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView).offset(16);
        make.size.equalTo(@(kAvatarImageSize));
    }];
    
    [_authorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.avatarImageView).offset(4);
        make.left.equalTo(weakSelf.avatarImageView.mas_right).offset(10);
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.authorNameLabel.mas_right).offset(10);
        make.centerY.equalTo(weakSelf.authorNameLabel);
    }];
    
    [_likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.centerY.equalTo(weakSelf.authorNameLabel);
        make.size.mas_equalTo(CGSizeMake(15.0f, 16.5f));
    }];
    
    [_likeNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.likeButton.mas_left).offset(-5);
        make.centerY.equalTo(weakSelf.likeButton);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.authorNameLabel.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.authorNameLabel.mas_left);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-25);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(-16);
    }];
}

- (void)reconfigureConstraints
{
    WeakSelf;
    if ([self.comment isKindOfClass:[FQDChildComment class]]) {
        [_avatarImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView).offset(0);
            make.left.equalTo(weakSelf.contentView).offset(59);
        }];
    } else {
        [_avatarImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.contentView).offset(16);
            make.left.equalTo(weakSelf.contentView).offset(16);
        }];   
    }
}

- (void)likeComment:(id)sender
{
    if (self.delegate) {
        [self.delegate commentCell:self didClickedLikeButton:sender comment:self.comment];
    }
}

#pragma mark - Setters and Getters
- (void)setComment:(FQDComment *)comment
{
    _comment = comment;
    
    [self reconfigureConstraints];
    
    NSURL *avatarURL = [NSURL URLWithString:comment.author.avatar];
    [self.avatarImageView sd_setImageWithURL:avatarURL];
    self.authorNameLabel.text = comment.author.name;
    self.dateLabel.text = [[NSDate dateWithTimeIntervalSince1970:comment.publish_time] dateStringSinceNow];
    
    NSInteger praiseCount = comment.praise_count;
    NSString *likeNumStr = (praiseCount > 0) ? [@(praiseCount) stringValue] : @"";
    self.likeNumLabel.text = likeNumStr;
    
    if ([self.comment isKindOfClass:[FQDChildComment class]]) {
        FQDChildComment *childComment = (FQDChildComment *)comment;
        NSString *replyTo = [NSString stringWithFormat:@"@%@:", childComment.parent_user.name];
        self.contentLabel.text = [NSString stringWithFormat:@"%@ %@", replyTo, childComment.content];
    } else {
        self.contentLabel.text = comment.content;
    }
    
    FQDPraiseHistory *praiseHistory = [FQDPraiseHistory sharedHistory];
    FQDAccountManager *accountManager = [FQDAccountManager sharedManager];
    NSString *userId = accountManager.currentUserId;
    BOOL liked = [praiseHistory userHasPraised:comment.ID userId:userId];
    self.liked = liked;
   
}

- (void)setLiked:(BOOL)liked
{
    _liked = liked;
    
    if (liked) {
        UIImage *likedImage = [UIImage imageNamed:@"commentLiked"];
        [self.likeButton setBackgroundImage:likedImage forState:UIControlStateNormal];
    } else {
        UIImage *likeImage = [UIImage imageNamed:@"commentLike"];
        [self.likeButton setBackgroundImage:likeImage forState:UIControlStateNormal];
    }
}

+ (NSString *)reuseId
{
    return @"FQDCommentCell";
}

@end
